<?php
/**
 * 
 * @author Paolo Bertuzzo
 *
 */
namespace User\Controller;

use User\Service\CustomerService;
use Zend\View\Model\ViewModel;
use Zend\Log\Logger;
use Utilities\Session\SessionFacade;
use Zend\Mvc\Controller\AbstractActionController;

class AuthenticationController extends AbstractActionController
{

    /**
     */
    protected $logger;
    
    /**
     */
    protected $customerService;

    /**
     * 
     * @param CustomerService $customerService
     * @param Logger $logger
     */
    public function __construct(CustomerService $customerService, Logger $logger)
    {
        $this->customerService = $customerService;
        $this->logger = $logger;
    }

    /**
     */
    public function authenticateAction() 
    {
        // Save current URL
        $sessionFacade = new SessionFacade();
        
        $sessionFacade->setCurrentUrl($this->params()->fromQuery('url'));
        
        // Forward request to OpAuth
        return $this->redirect()->toRoute('lfjopauth_login', array('provider' => 'facebook'));
    }
    
    /**
     */
    public function deauthenticateAction()
    {
        // Save current URL
    	$sessionFacade = new SessionFacade();
    
    	$sessionFacade->setCurrentUrl($this->params()->fromQuery('url'));
    
    	// Forward request to OpAuth
    	return $this->redirect()->toRoute('lfjopauth_logout', array('provider' => 'facebook'));
    }

    /**
     */
    public function deauthenticateCallbackAction()
    {
        $this->customerService->logout();

    	// Redirect user to last visited URL
        $sessionFacade = new SessionFacade();
        
    	return $this->redirect()->toUrl($sessionFacade->getCurrentUrl());
    }
    
    /**
     */
    public function authenticateFromFacebookCallbackAction()
    {
        $sessionFacade = new SessionFacade();
        
        // Retrieve any authentication error
        $authenticationError = $sessionFacade->getAuthenticationError();
        
        // If errors occurred, redirect to error page
        if ($authenticationError != null) {
            $this->logger->err('Error while authenticating user.', $authenticationError);
            
            // Forward user to the error page
            return $this->forward()->dispatch('authentication', array('action' => 'error'));
        }
            
        // Retrieve user data from OpAuth authentication
        $authenticationInfo = $sessionFacade->getAuthenticationInfo();
        
    	$this->customerService->loginFromAuthenticationProvider($authenticationInfo['nickname'], $authenticationInfo['first_name'], 
    	    $authenticationInfo['last_name'], $authenticationInfo['email'], $this->params()->fromRoute('provider'));
    	
    	// Redirect user to last visited URL
    	return $this->redirect()->toUrl($sessionFacade->getCurrentUrl());
    }
    
    /**
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function errorAction()
    {
    	return new ViewModel();
    }
    
}

?>